package cn.tedu;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

public class HBaseJavaClient {

    /**
     * hbase操作必备
     * @return
     */
    private static Configuration getConfiguration() {
        // 获得HBase配置
        Configuration conf = HBaseConfiguration.create();
        // 连接HBase
        conf.set("hbase.rootdir", "hdfs://hadoop01:9000/hbase");
        // 使用eclipse时必须添加这个，否则无法定位
        conf.set("hbase.zookeeper.quorum", "hadoop01");
        return conf;
    }

    /**
     * 创建一张表
     * @param tableName
     * @param columnFamily
     * @throws IOException
     */
    public static void create(String tableName, String columnFamily) throws IOException {
        // 获取HBase的管理权
        HBaseAdmin admin = new HBaseAdmin(getConfiguration());
        if (admin.tableExists(tableName)) {
            System.out.println("table exists!");
        } else {
            // 创建Table描述器
            HTableDescriptor tableDesc = new HTableDescriptor(tableName);
            // 创建列描述器
            HColumnDescriptor hcd = new HColumnDescriptor(columnFamily);
            // 添加列族
            tableDesc.addFamily(hcd);
            // 建表
            admin.createTable(tableDesc);
            System.out.println("create table success!");
        }
    }

    /**
     * 添加数据
     * @param tableName
     * @param row
     * @param columnFamily
     * @param column
     * @param data
     * @throws IOException
     */
    public static void put(String tableName, String row, String columnFamily, String column, String data)
            throws IOException {
        // 连接表
        HTable table = new HTable(getConfiguration(), tableName);
        // 将 数据封装成一个 put对象
        // 参数表示行键
        Put p1 = new Put(Bytes.toBytes(row));
        // 列族/列簇 列名 值
        // p1.add(family, qualifier, value);
        p1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(data));
        // 添加数据
        table.put(p1);
        System.out.println("put'" + row + "'," + columnFamily + ":" + column + "','" + data + "'");
    }

    /**
     *  查询数据
     * @param tableName
     * @param row
     * @param family
     * @param qualifier
     * @throws IOException
     */
    public static void get(String tableName, String row, String family, String qualifier) throws IOException {
        // 连接表
        HTable table = new HTable(getConfiguration(), tableName);
        // 参数表示行键
        Get get = new Get(Bytes.toBytes(row));
        // 查询数据
        Result result = table.get(get);
        // 获取值
        byte[] bs = result.getValue(family.getBytes(), qualifier.getBytes());
        System.out.println("Get: " + new String(bs));
    }

    /**
     * 显示所有数据
     * @param tableName
     * @throws IOException
     */
    public static void scan(String tableName) throws IOException {
        // 连接表
        HTable table = new HTable(getConfiguration(), tableName);
        // 创建扫描器
        Scan scan = new Scan();
        /*
         * 进行扫描 扫描返回的结果不止一条
         */
        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {
            byte[] value = result.getValue("apics".getBytes(), "newc".getBytes());
            if (value != null)
                System.out.println(new String(value));
            System.out.println("Scan: " + result);
        }
    }

    /**
     * 删除表
     * @param tableName
     * @throws IOException
     */
    public static void delete(String tableName) throws IOException {
        // 获取管理权
        HBaseAdmin admin = new HBaseAdmin(getConfiguration());
        // 判断表是否存在
        if (admin.tableExists(tableName)) {
            try {
                // 删除前需要先进行disable表
                admin.disableTable(tableName);
                // 删除表
                admin.deleteTable(tableName);
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("Delete " + tableName + " 失败");
            }
        }
        System.out.println("Delete " + tableName + " 成功");
    }

    /**
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        create("apitable", "apics");
//        put("apitable", "NewUser", "apics", "newc", "Jerry");
//        get("apitable", "NewUser", "apics", "newc");
//        scan("apitable");
//        delete("apitable");
    }
}

